package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.AttributeMapper;
import com.auemall.dao.AttributeValuesMapper;
import com.auemall.dao.ProductAttributeMapper;
import com.auemall.dao.ProductinfoMapper;
import com.auemall.po.Attribute;
import com.auemall.po.AttributeValues;
import com.auemall.po.ProductAttribute;
import com.auemall.po.Productinfo;
import com.auemall.service.AttributeService;
import com.auemall.vo.AttributesAndValues;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AttributeServiceImpl implements AttributeService {

	@Autowired
	private AttributeMapper attributeMapper;
	
	@Autowired
	private AttributeValuesMapper attributeValuesMapper;
	
	@Autowired
	private ProductAttributeMapper productAttributeMapper;
	
	@Autowired
	private ProductinfoMapper productinfoMapper;
	
	
	public List<AttributesAndValues> findAttributeByCatageryId(Integer catageryId,String strs,HttpServletRequest request,Integer currentPage){
		
		List<Attribute> list = attributeMapper.findAllAttributeByCategoryId(catageryId);
		
		List<Integer> ids = new ArrayList<Integer>();
		
		for (Attribute att:list) {
			
			ids.add(att.getId());
		}
		
		if (ids.size() > 0) {
			
		   List<AttributeValues> listValues = this.attributeValuesMapper.findAllAttributeValuesByCategoryId(ids);	
		   
		   List<AttributesAndValues> values = new ArrayList<AttributesAndValues>();
		   
		   for (Attribute attribute:list) {
			   
			   Integer aid = attribute.getId();
			   
			   AttributesAndValues aandV = new AttributesAndValues();
			   
			   aandV.setAttributeId(aid);
			   
			   aandV.setAttribute(attribute.getAttributeName());
			   
			   aandV.setCategory_id(attribute.getCategoryId());
			   
			   List<AttributeValues> attvs = new ArrayList<AttributeValues>();
			   
			   for (AttributeValues value:listValues) {
				   
				    if (aid.intValue() == value.getAttributeId().intValue()) {
				    	
				    	attvs.add(value);
				    }
				       
			   }
			   
			   aandV.setValues(attvs);
			   
			   values.add(aandV);
		   }
		   
		   
		   //查询对应的产品信息
		   List<Integer> vids = new ArrayList<Integer>();
		   
		   List<ProductAttribute> valuesList = null;
		   
		   if (strs != null && !"".equals(strs)) {
			   
			  String [] array = strs.split(",");   
			  
			  for (String s:array) {
				  
				  vids.add(Integer.parseInt(s));
			  }
			  
			//通过属性值查询对应的信息
			valuesList = this.productAttributeMapper.findAllAttributeByValues(vids);
			  
		   }else {
			   
			   //valuesList = this.productAttributeMapper.findAllAttributeValues();
		   } 
		   
		   List<Productinfo> productLists = null;
		   
			//通过产品属性值查询对应的产品
		    if (valuesList != null && valuesList.size() > 0 ) {
			 	  
				  List<Integer> proids = new ArrayList<Integer>();
				  
				  for (ProductAttribute pa:valuesList) {
					  
					  proids.add(pa.getProductId());
				  }
				  
				   //分页产品
					
					if (currentPage == null || currentPage <=0) {
						
						currentPage = 1;
					}
					
			  PageHelper.startPage(currentPage, ApplicationConstant.PAGE_SIZE); 
			      
			  productLists = this.productinfoMapper.findProductInfoByValues(proids);
			  
			  PageInfo<Productinfo> pageInfo = new PageInfo<Productinfo>(productLists);
				
			  request.setAttribute("pageview",pageInfo);
			 
			}else {
				
				productLists = new ArrayList<Productinfo>();
				
				PageInfo<Productinfo> pageInfo = new PageInfo<Productinfo>(productLists);
				
				request.setAttribute("pageview",pageInfo);
			}
		   
		   return values;
		   
			
		}else {
			
			return null;
		}
		
		
	}
}
